package com.greate.community.event;

import com.alibaba.fastjson.JSONObject;
import com.greate.community.entity.Event;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

/**
 * 事件的生产者
 */
@Component
public class EventProducer {

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 处理事件
     * @param event
     */
    public void fireEvent(Event event)  {
        Message message = new Message(event.getTopic(),JSONObject.toJSONBytes(event));
        try {
            rocketMQTemplate.getProducer().send(message);
        } catch (MQClientException e) {
            e.printStackTrace();
        } catch (RemotingException e) {
            e.printStackTrace();
        } catch (MQBrokerException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
//    public void fireEvent(Event event) {
//        // 将事件发布到指定的主题
//        kafkaTemplate.send(event.getTopic(), JSONObject.toJSONString(event));
//    }

}
